// Advnc_Swaper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
// System headers
#include <iostream>
#include "stdhdrs.h"

////////////////////////////
// Custom headers
#include "Advnc_Swapper.h"
#include "VSocket.h"
#include "vncServer.h"
#include "vncInstHandler.h"
#include "VNCviewerApp32.h"

const char	*szAppName = "ADVNC";

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
   // Initialize the VSocket system
   VSocketSystem socksys;
   if (!socksys.Initialized()) {
      MessageBox(NULL, "Failed to initialize the socket system", szAppName, MB_OK | MB_ICONSTOP);
      return 0;
   }
   vnclog.Print(LL_STATE, VNCLOG("sockets initialized\n"));

   VNCviewerApp32 app(hInstance, szCmdLine);

   return AdVNCAppMain();
}

int AdVNCAppMain()
{
   // Set this process to be the last application to be shut down.
   SetProcessShutdownParameters(0x100, 0);

   // Check for previous instances of AdVNC!
   vncInstHandler instancehan;
   if (!instancehan.Init()) {
      // We don't allow multiple instances!
      MessageBox(NULL, "Another instance of AdVNC-swapper is already running!", szAppName, 
         MB_OK | MB_ICONSTOP);
      return 0;
   }

   // CREATE SERVER(Controlled)
   vncServer server;
   // Set the name and port number
   server.SetName(szAppName);
   server.SockConnect(TRUE);
   vnclog.Print(LL_STATE, VNCLOG("server created ok\n"));

   // Now enter the message handling loop until told to quit!
   MSG msg;
   while (GetMessage(&msg, NULL, 0,0) ) {
      vnclog.Print(LL_INTINFO, VNCLOG("message %d received\n"), msg.message);
      TranslateMessage(&msg);	// convert key ups and downs to chars
      DispatchMessage(&msg);
   }

   return msg.wParam;
}
